package com.wx.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wx.mapper.OrderDetailMapper;
import com.wx.mapper.SourceDetailMapper;
import com.wx.pojo.OrderDetail;
import com.wx.pojo.Source;
import com.wx.pojo.SourceDetail;
import com.wx.service.OrderDetailService;
import com.wx.service.SourceDetailService;
/**
 * 订单商品详细信息
 * @author gzcadmin
 *
 */
@Service
public class OrderDetailServiceImpl implements OrderDetailService {

	@Autowired
	private OrderDetailMapper orderDetailMapper;

	@Autowired
	private SourceDetailMapper sourceDetailMapper;
	
	@Autowired
	private SourceDetailService sourceDetailService;
	/**
	 * 创建订单商品详细信息
	 */
	@Override
	public Boolean createOrderDetailForm(List<OrderDetail> orderDetailList, String orderId) {
		Boolean flag=true;
		for (OrderDetail orderDetail : orderDetailList) {
			orderDetail.setOrderId(orderId);
			orderDetailMapper.createOrderDetail(orderDetail);
			if(orderDetail.getOrderDetailId()==null) {
				flag=false;
				return flag;
			}
			SourceDetail sourceD=sourceDetailService.findSourceById(orderDetail.getSourceId());
			if(sourceD.getSource_count()<orderDetail.getSourceCount()) {
				flag=false;
				return flag;
			}
			Source source=new Source();
			source.setSourceId(orderDetail.getSourceId());
			source.setSource_count(sourceD.getSource_count()-orderDetail.getSourceCount());
			sourceDetailMapper.updateSourceCountBysourceId(source);
		}
		return flag;
	}
	/**
	 * 通过订单id查询订单商品的详细信息
	 */
	@Override
	public List<OrderDetail> findOrderDetailByOrderId(String orderId) {
		return orderDetailMapper.findOrderDetailByOrderId(orderId);
	}


	

}
